import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/login.vue'
import Register from '@/components/Register.vue'
import Home from '@/components/Home.vue'
import PcIndex from '@/components/PcIndex.vue'
import MbIndex from '@/components/MbIndex.vue'
import Welcome from '@/components/Welcome.vue'
import UserList from '@/components/userAdmin/UserList.vue'
import Category from '@/components/category/Category.vue'
import WriteArticle from '@/components/article/WriteArticle.vue'
import AllArcticle from '@/components/article/AllArticle.vue'
import MyArticle from '@/components/article/MyArticle.vue'
import AwiatCheck from '@/components/article/AwiatCheck.vue'

Vue.use(Router)
function _isMobile() {
  let flag = navigator.userAgent.match(
    /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
  )
  return flag
}
const url = _isMobile() ? '/mb_index' : '/pc_index'

const router = new Router({
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      redirect: url
    },
    {
      path: "/pc_index", // pc端首页
      name: 'PcIndex',
      component: PcIndex
    },
    {
      path: '/mb_index', // 手机端首页
      name: 'MbIndex',
      component: MbIndex
    },
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/register',
      name: 'Register',
      component: Register
    },
    {
      path: '/home',
      name: 'Home',
      component: Home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', name: 'Welcome', component: Welcome },
        { path: '/userList', name: 'UserList', component: UserList },
        { path: '/category', name: 'Category', component: Category },
        { path: '/writeArticle', name: 'WriteArticle', component: WriteArticle },
        { path: '/allArcticle', name: 'AllArcticle', component: AllArcticle },
        { path: '/myArticle', name: 'MyArticle', component: MyArticle },
        { path: '/awiatCheck', name: 'AwiatCheck', component: AwiatCheck }
      ]
    }

  ]
})
router.beforeEach((to, from, next) => {
  var isLogin = window.sessionStorage.getItem("token") ? true : false
  if (to.path == '/login' || to.path == '/register' || to.path == '/' || to.path == '/pc_index' || to.path == '/mb_index') {
    next();
  } else {
    isLogin ? next() : next('/login')
  }

})
export default router;
